package com.gljx.web.service.impl;

import com.gljx.web.Entity.JournalEntry.DepDayCompleteJournal;
import com.gljx.web.Entity.JournalEntry.DepDayCompleteJournal2;
import com.gljx.web.Entity.JournalEntry.StudentStateJournal;
import com.gljx.web.Entity.Student;
import com.gljx.web.dao.*;
import com.gljx.web.service.JournalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.Collator;
import java.util.*;

/**
 * Created by Administrator on 2017/12/22 0022.
 */
@Transactional
@Service
/**
 * @Author lcx
 * @Date 2017/12/29 0029 下午 3:21
 *
 * 统计学员的 报表业务类
 */
public class JournalServiceImpl implements JournalService {
    @Autowired
    SharecartworegMapper sharecartworegMapper;
    @Autowired
    SubjectTwoResultMapper subjectTwoResultMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ExamsubscribeMapper examsubscribeMapper;
    @Autowired
    SubjectOneResultMapper subjectOneResultMapper;

    @Override
    public List<StudentStateJournal> selectStudentState(Map map) {


        List<StudentStateJournal> reList = new ArrayList<>();
        Set<String> sets = new LinkedHashSet<>();

        //测试用
        /*map.put("beginTime", "2017-01-01");
        map.put("endTime", "2017-12-30");*/

        //查询科目一二三明细 根据传入的 table
        List<StudentStateJournal> studentStateJournals = sharecartworegMapper.selectmxJournal(map);

        //如果是科目一的查询
        if ("1".equals(map.get("table"))) {

            //查询小车标识
            map.put("fig", null);
            //查询已经报名的
            //todo 暂时没有数据
            List<StudentStateJournal> ybms = studentMapper.selectEnrol(map);
            //已约科目一
            List<StudentStateJournal> ksyys = examsubscribeMapper.selectKmy(map);
            //已考科目一不及格 科目一及格
            List<StudentStateJournal> ksjgs = subjectOneResultMapper.selectmxJournal(map);

            map.put("fig", 1);
            List<StudentStateJournal> ybmb = studentMapper.selectEnrol(map);
            List<StudentStateJournal> ksyyb = examsubscribeMapper.selectKmy(map);
            List<StudentStateJournal> ksjgb = subjectOneResultMapper.selectmxJournal(map);

            //初始化
            fomatSet(sets, studentStateJournals);
            fomatSet(sets, ybms);
            fomatSet(sets, ybmb);
            fomatSet(sets, ksyys);
            fomatSet(sets, ksyyb);
            fomatSet(sets, ksjgs);
            fomatSet(sets, ksjgb);

            for (String tem : sets) {
                StudentStateJournal studentStateJournal = new StudentStateJournal();
                studentStateJournal.setFbizdate(tem);
                reList.add(studentStateJournal);
            }

            fomatData(reList, studentStateJournals, 0);
            fomatData(reList, ybms, 5);
            fomatData(reList, ybmb, 6);
            fomatData(reList, ksyys, 7);
            fomatData(reList, ksyyb, 8);
            fomatData(reList, ksjgs, 9);
            fomatData(reList, ksjgb, 10);
            return reList;
        }
        //查询小车标识
        map.put("fig", null);
        //查询科目二未培训（科目二练车中）小车
        List<StudentStateJournal> lczs = sharecartworegMapper.selectNotTraining(map);
        //已培训（考试通过）tem1 合格 tem2（不及格）tem3（未到）
        List<StudentStateJournal> ssmall = subjectTwoResultMapper.selectmxJournal(map);

        //查询大车标识
        map.put("fig", 1);
        //查询科目二未培训（科目二练车中）小车
        List<StudentStateJournal> lczb = sharecartworegMapper.selectNotTraining(map);
        //查大车 已培训（考试通过）tem1 未培训tem2（不及格）tem3（未到）
        List<StudentStateJournal> ypxb = subjectTwoResultMapper.selectmxJournal(map);


        //初始化set
        fomatSet(sets, studentStateJournals);
        fomatSet(sets, ssmall);
        fomatSet(sets, ypxb);
        fomatSet(sets, lczs);
        fomatSet(sets, lczb);

        for (String tem : sets) {
            StudentStateJournal studentStateJournal = new StudentStateJournal();
            studentStateJournal.setFbizdate(tem);
            reList.add(studentStateJournal);
        }

        //初始化集合
        fomatData(reList, studentStateJournals, 0);
        //大小车科目二已培训 和 大小车科目二及格
        fomatData(reList, ssmall, 1);
        fomatData(reList, ypxb, 2);
        fomatData(reList, lczs, 3);
        fomatData(reList, lczb, 4);

        return reList;

    }

    /**
     * @Author lcx
     * @Date 2017/12/27 0027 上午 10:11
     * 部门日招生完成情况统计表
     */
    @Override
    public List<DepDayCompleteJournal> selectDepAdmissions(Map map) {
        List<DepDayCompleteJournal> depDayCompleteJournals = studentMapper.selectDepAdmissions(map);
        return depDayCompleteJournals;
    }

    /**
     * @Author lcx
     * @Date 2017/12/27 0027 下午 4:11
     * 这两个方法 为了 组装报表数据 实在没办法 所以分次取数据 在拼装
     */
    //组装数据set
    private void fomatSet(Set<String> sets, List<StudentStateJournal> studentStateJournals) {
        for (StudentStateJournal tem : studentStateJournals) {
            sets.add(tem.getFbizdate());
        }
    }

    //组装数据
    private List<StudentStateJournal> fomatData(List<StudentStateJournal> reList, List<StudentStateJournal> studentStateJournals, int state) {

        for (StudentStateJournal tem : studentStateJournals) {
            for (StudentStateJournal tem2 : reList) {
                if (state == 0 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setCarTypeA1(tem.getCarTypeA1());
                    tem2.setCarTypeA2(tem.getCarTypeA2());
                    tem2.setCarTypeA3(tem.getCarTypeA3());
                    tem2.setCarTypeB1(tem.getCarTypeB1());
                    tem2.setCarTypeB2(tem.getCarTypeB2());
                    tem2.setCarTypeC1(tem.getCarTypeC1());
                    tem2.setCarTypeC2(tem.getCarTypeC2());

                }
                //大小车科目二已培训 和 大小车科目二及格
                else if (state == 1 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setYpxSmallCar(tem.getTem1());

                    tem2.setKmehgSmallCar(tem.getTem1());
                    //不及格
                    tem2.setKmebhgSmallCar(tem.getTem2());
                    //缺考
                    tem2.setKmewdSmallCar(tem.getTem3());
                } else if (state == 2 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setYpxBigCar(tem.getTem1());
                    tem2.setKmehgBigCar(tem.getTem1());
                    //不及格
                    tem2.setKmebhgBigCar(tem.getTem2());
                    tem2.setKmewdBigCar(tem.getTem3());

                }
                //小车（练车中）
                else if (state == 3 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setWpxSmallCar(tem.getTem1());
                }
                //大车（练车中）
                else if (state == 4 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setWpxBigCar(tem.getTem1());
                }
                //科目一已报名(小车)
                else if (state == 5 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setKmyybmSmallCar(tem.getTem1());
                }
                //科目一已报名(大车)
                else if (state == 6 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setKmyybmBigCar(tem.getTem1());
                }
                //已约科目一(小车)
                else if (state == 7 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setKmyyySmallCar(tem.getTem1());
                }
                //已约科目一(大车)
                else if (state == 8 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setKmyyyBigCar(tem.getTem1());
                }
                //已考科目一不及格(小车) tem1 为及格
                else if (state == 9 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setKmyjgSmallCar(tem.getTem1());
                    tem2.setKmybjgSmallCar(tem.getTem2());
                }
                //已约科目一(大车)
                else if (state == 10 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                    tem2.setKmyjgBigCar(tem.getTem1());
                    tem2.setKmybjgBigCar(tem.getTem2());
                } else {
                }
            }
        }
        return reList;
    }


    /**
     * @Author lcx
     * @Date 2017/12/27 0027 上午 11:29
     * 本年累计 部门日招生完成情况统计表
     */
    @Override
    public List<DepDayCompleteJournal> selectDepAdmissionsAsYears(Map map) {
        //获取年
        String year = map.get("beginTime").toString().substring(0, 4);
        map.put("beginTime", year + "-01");
        map.put("endTime", year + "-12");

        List<DepDayCompleteJournal> depDayCompleteJournals = studentMapper.selectDepAdmissionsAsYears(map);
        return depDayCompleteJournals;
    }


    /**
     * @Author lcx
     * @Date 2017/12/28 0028 上午 9:01
     * 部门月度招生完成情况统计表
     */
    @Override
    public List<DepDayCompleteJournal2> selectDepAdmissionAsDepTjr(Map map) {

        List<DepDayCompleteJournal2> reList = new ArrayList<>();
        Set<String> sets = new LinkedHashSet<>();

        //获取年
        String year = map.get("beginTime").toString().substring(0, 4);
        map.put("beginTime", year + "-01");
        map.put("endTime", year + "-12");

        //查询小车标识
        map.put("fig", null);

        //查询个人月度招生完成情况统计表
        List<DepDayCompleteJournal2> depDayCompleteJournal2s = studentMapper.selectDepAdmissionsAsYears2(map);

        //查询大车标识
        map.put("fig", 1);
        List<DepDayCompleteJournal2> depDayCompleteJournal2b = studentMapper.selectDepAdmissionsAsYears2(map);

        fomatSet2(sets, depDayCompleteJournal2s);
        fomatSet2(sets, depDayCompleteJournal2b);

        for (String tem : sets) {
            DepDayCompleteJournal2 depDayCompleteJournal2 = new DepDayCompleteJournal2();
            depDayCompleteJournal2.setFid(tem);
            //如果推荐人为空则排除
            if (tem == null) {
                continue;
            }
            reList.add(depDayCompleteJournal2);
        }

        fomatData2(reList, depDayCompleteJournal2s, 1);
        fomatData2(reList, depDayCompleteJournal2b, 2);

        /*DepDayCompleteJournal2 s = new DepDayCompleteJournal2();
        s.setDepName("22");
        s.setName("1");
        DepDayCompleteJournal2 s1 = new DepDayCompleteJournal2();
        s1.setDepName(null);
        s1.setName("2");
        DepDayCompleteJournal2 s2 = new DepDayCompleteJournal2();
        s2.setDepName(null);
        s2.setName("3");
        reList.add(s);
        reList.add(s);
        reList.add(s1);
        reList.add(s2);*/

        //排序
        reList.sort((o1, o2) -> {
            System.err.println("wo1:" + o1.getDepName());
            System.err.println("wo2:" + o2.getDepName());
            if (o1.getDepName() == null) {
                o1.setDepName("无");
            }
            if (o2.getDepName() == null) {
                o2.setDepName("无");
            }
            return o1.getDepName().compareTo(o2.getDepName());
        });
        //删除最后一条空数据
        //reList.remove(reList.size()-1);
        return reList;
    }

    /**
     * @Author lcx
     * @Date 2017/12/29 0029 下午 2:55
     * 部门月度招生完成情况统计表 （部门）
     */
    @Override
    public List<DepDayCompleteJournal2> selectDepAdmissionAsDep(Map map) {

        List<DepDayCompleteJournal2> reList = new ArrayList<>();
        Set<String> sets = new LinkedHashSet<>();

        //获取年
        String year = map.get("beginTime").toString().substring(0, 4);
        map.put("beginTime", year + "-01");
        map.put("endTime", year + "-12");

        //查询小车标识
        map.put("fig", null);

        //查询个人月度招生完成情况统计表
        List<DepDayCompleteJournal2> depDayCompleteJournal3s = studentMapper.selectDepAdmissionsAsYears3(map);

        //查询大车标识
        map.put("fig", 1);
        List<DepDayCompleteJournal2> depDayCompleteJournal3b = studentMapper.selectDepAdmissionsAsYears3(map);

        fomatSet3(sets, depDayCompleteJournal3s);
        fomatSet3(sets, depDayCompleteJournal3b);

        for (String tem : sets) {
            DepDayCompleteJournal2 depDayCompleteJournal2 = new DepDayCompleteJournal2();
            depDayCompleteJournal2.setDepName(tem);
            //如果部门空则排除
            if (tem == null) {
                continue;
            }
            reList.add(depDayCompleteJournal2);
        }

        fomatData3(reList, depDayCompleteJournal3s, 1);
        fomatData3(reList, depDayCompleteJournal3b, 2);

        return reList;
    }

    //组装数据set 根据人员的fid制作出一个不重复的set序列  为了保证 后面fomatData2 数据的时候 模版list 是正确的
    private void fomatSet2(Set<String> sets, List<DepDayCompleteJournal2> depDayCompleteJournal2s) {
        for (DepDayCompleteJournal2 tem : depDayCompleteJournal2s) {
            sets.add(tem.getFid());
        }
    }

    //组装数据2
    private List<DepDayCompleteJournal2> fomatData2(List<DepDayCompleteJournal2> reList, List<DepDayCompleteJournal2> depDayCompleteJournal2s, int state) {
        for (DepDayCompleteJournal2 tem : depDayCompleteJournal2s) {

            //排除 没有推荐人的学生
            if (tem.getFid() == null) {
                continue;
            }
            for (DepDayCompleteJournal2 tem2 : reList) {

                //小车
                if (state == 1 && tem.getFid().equals(tem2.getFid())) {
                    tem2.setDepName(tem.getDepName());
                    tem2.setName(tem.getName());
                    tem2.setSmallCar1(tem.getSmallCar1());
                    tem2.setSmallCar2(tem.getSmallCar2());
                    tem2.setSmallCar3(tem.getSmallCar3());
                    tem2.setSmallCar4(tem.getSmallCar4());
                    tem2.setSmallCar5(tem.getSmallCar5());
                    tem2.setSmallCar6(tem.getSmallCar6());
                    tem2.setSmallCar7(tem.getSmallCar7());
                    tem2.setSmallCar8(tem.getSmallCar8());
                    tem2.setSmallCar9(tem.getSmallCar9());
                    tem2.setSmallCar10(tem.getSmallCar10());
                    tem2.setSmallCar11(tem.getSmallCar11());
                    tem2.setSmallCar12(tem.getSmallCar12());
                } else if (state == 2 && tem.getFid().equals(tem2.getFid())) {
                    tem2.setDepName(tem.getDepName());
                    tem2.setName(tem.getName());
                    tem2.setBigCar1(tem.getBigCar1());
                    tem2.setBigCar2(tem.getBigCar2());
                    tem2.setBigCar3(tem.getBigCar3());
                    tem2.setBigCar4(tem.getBigCar4());
                    tem2.setBigCar5(tem.getBigCar5());
                    tem2.setBigCar6(tem.getBigCar6());
                    tem2.setBigCar7(tem.getBigCar7());
                    tem2.setBigCar8(tem.getBigCar8());
                    tem2.setBigCar9(tem.getBigCar9());
                    tem2.setBigCar10(tem.getBigCar10());
                    tem2.setBigCar11(tem.getBigCar11());
                    tem2.setBigCar12(tem.getBigCar12());
                }
            }
        }
        return reList;
    }

    //组装数据set
    private void fomatSet3(Set<String> sets, List<DepDayCompleteJournal2> depDayCompleteJournal2s) {
        for (DepDayCompleteJournal2 tem : depDayCompleteJournal2s) {
            sets.add(tem.getDepName());
        }
    }

    //组装数据2
    private List<DepDayCompleteJournal2> fomatData3(List<DepDayCompleteJournal2> reList, List<DepDayCompleteJournal2> depDayCompleteJournal2s, int state) {
        int i = 0;
        for (DepDayCompleteJournal2 tem : depDayCompleteJournal2s) {

            //排除 没有推荐人的学生
            if (tem.getDepName() == null) {
                continue;
            }
            for (DepDayCompleteJournal2 tem2 : reList) {

                //小车
                if (state == 1 && tem.getDepName().equals(tem2.getDepName())) {
                    tem2.setSmallCar1(tem.getSmallCar1());
                    tem2.setSmallCar2(tem.getSmallCar2());
                    tem2.setSmallCar3(tem.getSmallCar3());
                    tem2.setSmallCar4(tem.getSmallCar4());
                    tem2.setSmallCar5(tem.getSmallCar5());
                    tem2.setSmallCar6(tem.getSmallCar6());
                    tem2.setSmallCar7(tem.getSmallCar7());
                    tem2.setSmallCar8(tem.getSmallCar8());
                    tem2.setSmallCar9(tem.getSmallCar9());
                    tem2.setSmallCar10(tem.getSmallCar10());
                    tem2.setSmallCar11(tem.getSmallCar11());
                    tem2.setSmallCar12(tem.getSmallCar12());
                } else if (state == 2 && tem.getDepName().equals(tem2.getDepName())) {
                    tem2.setBigCar1(tem.getBigCar1());
                    tem2.setBigCar2(tem.getBigCar2());
                    tem2.setBigCar3(tem.getBigCar3());
                    tem2.setBigCar4(tem.getBigCar4());
                    tem2.setBigCar5(tem.getBigCar5());
                    tem2.setBigCar6(tem.getBigCar6());
                    tem2.setBigCar7(tem.getBigCar7());
                    tem2.setBigCar8(tem.getBigCar8());
                    tem2.setBigCar9(tem.getBigCar9());
                    tem2.setBigCar10(tem.getBigCar10());
                    tem2.setBigCar11(tem.getBigCar11());
                    tem2.setBigCar12(tem.getBigCar12());
                }
            }
        }
        return reList;
    }

}

